package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public User findByUsername(String username) {
        User user = userMapper.findByUsername(username);

        //根据用户id查询对应所拥有的角色
        Integer userId = user.getId();
        Set<Role> roles = roleMapper.findByUserId(userId);
        for (Role role : roles) {
            //根据角色id查询对应的权限
            Set<Permission> permissions = permissionMapper.findByRoleId(role.getId());
            //角色关联对应的权限
            role.setPermissions(permissions);
        }
        //用户关联对应的角色
        user.setRoles(roles);
        return user;
    }



}
